package cn.edu.nuc.ssm.controller;

import cn.edu.nuc.ssm.entity.User;
import cn.edu.nuc.ssm.service.AccountService;
import cn.edu.nuc.ssm.service.UserService;
import cn.edu.nuc.ssm.util.LogUtil;
import cn.edu.nuc.ssm.util.StringUtil;
import cn.edu.nuc.ssm.util.VerifyCodeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by Administrator on 2017/4/6.
 */
@Controller
@RequestMapping("/front")
public class FrontRegistController {

    private static Logger logger = LoggerFactory.getLogger(FrontRegistController.class);

    @Autowired
    private UserService userService;
    @Autowired
    private AccountService accountService;

    @RequestMapping(value = "/authimage",method = RequestMethod.GET)
    public void createValid(HttpServletResponse response,
                            HttpSession session){
        response.setHeader("Pragma","No-cache");
        response.setHeader("Cache-Control","no-cache");
        response.setDateHeader("Expires",0);
        response.setContentType("image/jpeg");

        //生成随机字串
        String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
        logger.debug("生成的验证是:{}",verifyCode);


        session.removeAttribute("verfycode");
        session.setAttribute("verfycode",verifyCode.toLowerCase());
        //生成图片
        int w = 100, h = 30;
        try {
            VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode);
        } catch (IOException e) {
            LogUtil.getTrace(e);
        }
    }

    @RequestMapping(value = "/validusername",method = RequestMethod.GET)
    public void validUsername(String username, HttpServletResponse httpResponse){

        int i = 0;
        try {
            i = userService.validUsername(username);
        } catch (Exception e) {
            logger.debug(LogUtil.getTrace(e));
            return;
        }
        if (i==0){
            try {
                StringUtil.writeToWeb("<font style='color:red;'>用户名已存在</font>","html",httpResponse);

            } catch (IOException e) {
                logger.debug(LogUtil.getTrace(e));
            }
        }else {
            try {
                StringUtil.writeToWeb("<font style='color:green;'>用户名可以注册</font>", "html", httpResponse);
            } catch (IOException e) {
                logger.debug(LogUtil.getTrace(e));
            }
        }
    }

    @RequestMapping(value = "/toregist")
    public String toRegist(){

        return "front/regist";
    }

    @RequestMapping(value = "/regist",method = RequestMethod.POST)
    public String regist(User user, Model model,HttpSession session,String validcode){

        logger.debug("接收的注删用户的信息是:{}",user);

        String sessioncode = (String) session.getAttribute("verfycode");
        logger.debug("session中的code中:{}",sessioncode);
        logger.debug("前台中的code中:{}",validcode);
        if (sessioncode==null){
            return "forward:/front/toregist";
        }
        if (!sessioncode.toLowerCase().equals(validcode.toLowerCase())){
            model.addAttribute("msg","<font color='red'>验证码输入不正确</font>");
            return "forward:/front/toregist";
        }

        try {

            int regist = userService.regist(user);

            if (regist<= 0){
                model.addAttribute("msg","数据发生错误");
                return "front/error/error";
            }
        } catch (Exception e) {
            model.addAttribute("msg","数据发生错误");
            logger.debug(LogUtil.getTrace(e));
            return "front/error/error";
        }

        accountService.initUserAccount(user.getId());


        session.removeAttribute("verfycode");
        return "front/login";
    }



}
